//
//  HCSlideController.m
//  hcpro
//
//  Created by 戴旭 on 16/9/12.
//  Copyright © 2016年 hucai.com. All rights reserved.
//

#import "HCSlideController.h"
#import "HCSlideViewModel.h"
#import "HCLeftMenuCell.h"
#import "HCBaseViewController.h"
#import "HCNaigationController.h"
#import "NSString+Volidate.h"

@interface HCSlideController()<UITableViewDelegate, UITableViewDataSource>
/** 未登录头部视图 */
@property(nonatomic, strong) UIView *unloginView;
/** 登录按钮 */
@property(nonatomic, strong) UIButton *btnlogin;
/** 注册按钮 */
@property(nonatomic, strong) UIButton *btnregist;

/** 已登录头部视图 */
@property(nonatomic, strong) UIView *loginView;
/** 用户信息 */
@property(nonatomic, strong) UILabel *lblUserInfo;
/** 余额信息 */
@property(nonatomic, strong) UILabel *lblBalance;

/** 菜单表视图 */
@property(nonatomic, strong) UITableView *slideTableView;

/** 菜单项数组 */
@property(nonatomic, strong) NSArray *menuItemArr;

/** 对应viewmodel */
@property(nonatomic, strong) HCSlideViewModel *vm;

/** 需显示视图索引 */
@property(nonatomic, assign) NSUInteger subviewIndex;

/** 保存所有顶级控制器 */
@property(nonatomic, strong) NSMutableArray<HCBaseViewController *> *topVCArrM;

/** 是否需要滑动动画*/
@property(nonatomic, assign) BOOL isNeedAnimation;
@end

static NSString *leftMenuRID = @"leftMenuReuseID";

@implementation HCSlideController

#pragma mark -
#pragma mark ----------------------------life cicle----------------------------
- (void) viewDidLoad{
    [super viewDidLoad];
    [self.view setBackgroundColor:HCBGColor];
    
    // 添加顶级控制器
//    [self setupAllChildViewControllers];
    // 默认添加第一项为顶级视图
    [self addChildViewControllerWithRow:0 andCol:0];
    
    // 添加子控件
    [self setupSubviews];
    
    // 注册重用cell
    [self.slideTableView registerClass:[HCLeftMenuCell class] forCellReuseIdentifier:leftMenuRID];
    
    // 默认显示第一个子控制器视图
    self.isNeedAnimation = NO;
    self.subviewIndex = 0;
}

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // 显示顶级控制器视图
    [self showTopViewWithIndex:self.subviewIndex];
}

#pragma mark -
#pragma mark ----------------------------setup----------------------------
/** 添加子视图 */
- (void) setupSubviews{
    // 已登录视图
    [self.view addSubview:self.loginView];
    [self.loginView addSubview:self.lblUserInfo];
    [self.loginView addSubview:self.lblBalance];
    self.loginView.hidden = YES;
    
    // 未登录视图
    [self.view addSubview:self.unloginView];
    [self.unloginView addSubview:self.btnlogin];
    [self.unloginView addSubview:self.btnregist];
   
    // 菜单表视图
    [self.view addSubview:self.slideTableView];
    
    // 布局
    __block CGFloat topViewW = HCSlideMenuW;
    __block CGFloat topViewH = 150;
    // 未登录视图
    [self.unloginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.view);
        make.width.equalTo(topViewW);
        make.height.equalTo(topViewH);
    }];
    // 登录按钮
    CGFloat marginH = 2 * HCMargin;
    __block CGFloat btnW = (topViewW - 3 * marginH) / 2;
    __block CGFloat btnH = 50;
    [self.btnlogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(btnW);
        make.height.equalTo(btnH);
        make.left.equalTo(self.unloginView).offset(marginH);
        make.centerY.equalTo(self.unloginView);
    }];
    // 注册按钮
    [self.btnregist mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(self.btnlogin);
        make.left.equalTo(self.btnlogin.right).offset(marginH);
        make.top.equalTo(self.btnlogin.top);
    }];
    // 已登录视图
    [self.loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.unloginView);
    }];
    // 菜单表视图
    [self.slideTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.unloginView.bottom).offset(HCMargin);
        make.left.bottom.equalTo(self.view);
        make.right.equalTo(self.unloginView.right);
    }];
}

/** 
 * 根据索引  添加子控制器
 * 返回值： 子控制器索引（topVCArrM）
 */
- (NSInteger) addChildViewControllerWithRow:(NSInteger)rowIndex andCol:(NSInteger)colIndex{
    // 拿到菜单项
    HCLeftMenuItem *menuItem = nil;
    id tempMenus = [self.menuItemArr objectAtIndex:rowIndex];
    if ([tempMenus isKindOfClass:[NSArray class]]) {
        menuItem = [((NSArray *)tempMenus) objectAtIndex:colIndex];
    }
    // 添加对于控制器
    NSInteger index = [self setupChildViewController:menuItem.menu_vcName];
    return index;
}

/** 
 *   根据对应控制器名称  设置子控制器
 *   返回值： 子控制器索引（topVCArrM）
 */
- (NSInteger) setupChildViewController:(NSString *)vcName{
    if ([vcName stringIsNullOrEmpty]) {
        return -1;
    }
    Class tempClass = NSClassFromString(vcName);
    id tempObj = [[tempClass alloc] init];
    if (![tempObj isKindOfClass:[UIViewController class]]) {
        NSLog(@"不是控制器类");
        return -1;
    }
    HCBaseViewController *vc = (HCBaseViewController *)tempObj;
    // 如果已存在 直接返回索引
    if ([self.topVCArrM containsObject:vc]) {
        NSInteger index = [self.topVCArrM indexOfObject:vc];
        return index;
    }
    // 不存在则添加
    [self.topVCArrM addObject:vc];
    HCNaigationController *nav = [[HCNaigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    // 返回索引
    return self.topVCArrM.count - 1;
}

#pragma mark -
#pragma mark ----------------------------user define---------------------------
/** 显示视图 */
- (void) showTopViewWithIndex:(NSInteger)index{
    __block HCBaseViewController *viewController = [self.childViewControllers objectAtIndex:index];
    [self.view addSubview:viewController.view];
    if (self.isNeedAnimation) {
        [viewController.view setHc_x:HCSlideMenuW];
        [UIView animateWithDuration:0.6 animations:^{
            [viewController.view setHc_x:0];
        }];
    }
}

#pragma mark -
#pragma mark ----------------------------event handle----------------------------
/** 登录按钮点击时间 */
- (void) btnLoginClick:(UIButton *)sender{
    
}
/** 注册按钮点击时间 */
- (void) btnRegistClick:(UIButton *)sender{
    
}

#pragma mark -
#pragma mark ----------------------------delegate datasource----------------------------
/** 代理方法 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.menuItemArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    id tempMenus = [self.menuItemArr objectAtIndex:section];
    if ([tempMenus isKindOfClass:[NSArray class]]) {
        return ((NSArray *)tempMenus).count;
    }
    else{
        return 10;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取cell
    HCLeftMenuCell *cell = [self.slideTableView dequeueReusableCellWithIdentifier:leftMenuRID];
    
    // 取模型
    HCLeftMenuItem *menuItem = nil;
    id tempMenus = [self.menuItemArr objectAtIndex:indexPath.section];
    if ([tempMenus isKindOfClass:[NSArray class]]) {
        menuItem = [((NSArray *)tempMenus) objectAtIndex:indexPath.row];
    }
    // 设置菜单内容
    cell.menuItem = menuItem;
    
    // 返回cell
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 点击的时候添加子控制器
    NSInteger index = [self addChildViewControllerWithRow:indexPath.section andCol:indexPath.row];
    index = index < 0 ? 0 : index;
    self.subviewIndex = index;
    // 需要滑动动画
    self.isNeedAnimation = YES;
    // 显示控制器视图
    [self showTopViewWithIndex:self.subviewIndex];
}
/** 设置行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
/** 设置组间距 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, HCMargin)];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HCMargin;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, HCMargin)];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return HCMargin;
}

#pragma mark -
#pragma mark ----------------------------lazyload(subviews)----------------------------
/** 未登录视图 */
- (UIView *)unloginView{
    if (_unloginView == nil) {
        _unloginView = [[UIView alloc] init];
        _unloginView.backgroundColor = HCTintColor;
    }
    return _unloginView;
}
/** 已登录视图 */
- (UIView *)loginView{
    if (_loginView == nil) {
        _loginView = [[UIView alloc] init];
        _loginView.backgroundColor = HCTintColor;
    }
    return _loginView;
}
/** 菜单表视图  */
- (UITableView *)slideTableView{
    if (_slideTableView == nil) {
        _slideTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _slideTableView.backgroundColor = HCBGColor;
        _slideTableView.dataSource = self;
        _slideTableView.delegate = self;
    }
    return _slideTableView;
}
/** 登录按钮 */
- (UIButton *)btnlogin{
    if (_btnlogin == nil) {
        _btnlogin = [[UIButton alloc] init];
        [_btnlogin.layer setCornerRadius:5.0];
        [_btnlogin setBackgroundColor:[UIColor whiteColor]];
        [_btnlogin setTitle:@"登 录" forState:UIControlStateNormal];
        [_btnlogin setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_btnlogin addTarget:self action:@selector(btnLoginClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnlogin;
}
/** 注册按钮 */
- (UIButton *)btnregist{
    if (_btnregist == nil) {
        _btnregist = [[UIButton alloc] init];
        [_btnregist.layer setCornerRadius:5.0];
        [_btnregist setBackgroundColor:[UIColor whiteColor]];
        [_btnregist setTitle:@"注 册" forState:UIControlStateNormal];
        [_btnregist setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_btnregist addTarget:self action:@selector(btnRegistClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnregist;
}
/** 用户信息 */
- (UILabel *)lblUserInfo{
    if (_lblUserInfo == nil) {
        _lblUserInfo = [[UILabel alloc] init];
    }
    return _lblUserInfo;
}
/** 余额信息 */
- (UILabel *)lblBalance{
    if (_lblBalance == nil) {
        _lblBalance = [[UILabel alloc] init];
    }
    return _lblBalance;
}

#pragma mark -
#pragma mark ----------------------------lazyload(property)----------------------------
/** viewmodel */
- (HCSlideViewModel *)vm{
    if (_vm == nil) {
        _vm = [[HCSlideViewModel alloc] init];
    }
    return _vm;
}

/** 获取菜单项数组*/
- (NSArray *)menuItemArr{
    _menuItemArr = [self.vm menuItemsArr];
    return _menuItemArr;
}

/** 获取顶级控制器 */
- (NSMutableArray<HCBaseViewController *> *)topVCArrM{
    if (_topVCArrM == nil) {
        _topVCArrM = [[NSMutableArray alloc] init];
    }
    return _topVCArrM;
}
@end
